//
//  H_HoneyChatViewController.m
//  Honey
//
//  Created by 贾亚涵 on 16/4/9.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_HoneyChatViewController.h"

@interface H_HoneyChatViewController ()<UITextFieldDelegate>
{
    UIView * _naviBarV;
    UIButton * _navBarBtn;
    
    UITextField * _textFil;
}

@end

@implementation H_HoneyChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initChatF];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self infoNavigationBar];

}



-(void)infoNavigationBar
{
    _naviBarV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 64)];
    _naviBarV.backgroundColor = [UIColor colorWithRed:246/255.0 green:157/255.0 blue:182/255.0 alpha:1];
    [self.view addSubview:_naviBarV];
    
    _navBarBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, 30, 30)];
    [_navBarBtn setImage:[UIImage imageNamed:@"HoneyChatD"] forState:UIControlStateNormal];
    [_navBarBtn addTarget:self action:@selector(BarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_naviBarV addSubview:_navBarBtn];
    
}

-(void)BarBtnClicked:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]]) {
        [self dismissViewControllerAnimated:YES completion:^{
    }];
    }
}

-(void)initChatF
{
    _textFil = [[UITextField alloc] initWithFrame:CGRectMake(0, Screen_Height-40, Screen_Width, 40)];
    _textFil.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_textFil];
    _textFil.borderStyle = UITextBorderStyleRoundedRect;
    _textFil.returnKeyType = UIReturnKeyDone;
    _textFil.enablesReturnKeyAutomatically = YES;
    _textFil.placeholder = @"请输入聊天内容";
    _textFil.delegate = self;
    
    //键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
   [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector (textFieldChange:) name:UITextFieldTextDidChangeNotification object:_textFil];

}

#pragma mark - 键盘 改变通知 弹键盘
-(void)keyboardWasChange:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    if ([[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y != [UIScreen mainScreen].bounds.size.height) {
        
        _textFil.frame = CGRectMake(0, [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y-45, [UIScreen mainScreen].bounds.size.width, 45);
        
    }else {
        _textFil.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 45, [UIScreen mainScreen].bounds.size.width, 45);
    }
}

//点击界面view 触发  收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_textFil resignFirstResponder];
    _textFil.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 45, [UIScreen mainScreen].bounds.size.width, 45);
}
-(void)textFieldChange:(NSNotification *)notification
{
    UITextField *textField=[notification object];
    
    NSString *lang = [textField.textInputMode primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
       // 获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if ([textField.text isEqualToString:@"王木木"]) {
                UIAlertView *ale = [[UIAlertView alloc]initWithTitle:@"我爱你" message:nil delegate:nil cancelButtonTitle:@"谢谢" otherButtonTitles: nil];
                [ale show];
            }
        }
        
    }
    
}
@end
